﻿Public Class Form1
    Dim AktTab As ExtendedWebBrowser
    Dim textfocus As Boolean = False

    Private Sub Progress(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs)
        ToolStripProgressBar1.Maximum = e.MaximumProgress
        If e.CurrentProgress = -1 Then
            ToolStripProgressBar1.Value = 0
        Else
            Try
                ToolStripProgressBar1.Value = e.CurrentProgress
            Catch
                ToolStripProgressBar1.Maximum = e.MaximumProgress
            End Try
        End If
    End Sub

    Private Sub Popup(ByVal sender As Object, ByVal e As WebBrowserNewWindowExtendedEventArgs)
        e.Cancel = True
        Dim url As String = e.Url
        Process.Start(My.Application.Info.DirectoryPath & "\" & My.Application.Info.Title, url)
    End Sub

    Private Sub SiteLaden()
        TabControl1.SelectedTab.Text = "Laden..."
    End Sub

    Private Sub BrowserFertig()
        If Not AktTab.DocumentTitle.ToString = Nothing Then
            TabControl1.SelectedTab.Text = AktTab.DocumentTitle
        Else
            TabControl1.SelectedTab.Text = AktTab.Url.ToString
        End If
        TextBox1.Text = AktTab.Url.ToString
        Dim ChronikPfad As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\glumeX\"
        If FileIO.FileSystem.DirectoryExists(ChronikPfad) = False Then
            My.Computer.FileSystem.CreateDirectory(ChronikPfad)
        End If
        My.Computer.FileSystem.WriteAllText(ChronikPfad & "chronik.ini", Date.Now & " " & " | " & AktTab.DocumentTitle & " | " & AktTab.Url.ToString & vbCrLf, True)
        Try
            Dim URL As Uri = New Uri(AktTab.Url.ToString)
            If URL.HostNameType = UriHostNameType.Dns Then
                Dim IconUrl = "http://" & URL.Host & "/favicon.ico"
                Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(IconUrl)
                Dim response As System.Net.HttpWebResponse = request.GetResponse
                Dim stream As System.IO.Stream = response.GetResponseStream
                Dim favicon = Image.FromStream(stream)
                PictureBox1.BackgroundImage = favicon
            End If
        Catch
            PictureBox1.BackgroundImage = Icon.ToBitmap
        End Try
    End Sub

    Public Sub NewTab(ByVal Name As String, ByVal URL As String)
        Dim tab As TabPage = New TabPage(Name)
        Dim wb As ExtendedWebBrowser = New ExtendedWebBrowser
        wb.Navigate(URL)
        wb.Dock = DockStyle.Fill
        tab.Controls.Add(wb)
        AddHandler wb.DocumentCompleted, AddressOf BrowserFertig
        AddHandler wb.Navigating, AddressOf SiteLaden
        AddHandler wb.NewWindowExtended, AddressOf Popup
        AddHandler wb.ProgressChanged, AddressOf Progress
        TabControl1.TabPages.Add(tab)
        TabControl1.SelectedTab = tab
        TextBox1.Focus()
        TextBox1.SelectAll()
    End Sub

    Public Sub GetAktTab()
        For Each wb As ExtendedWebBrowser In TabControl1.SelectedTab.Controls
            AktTab = wb
        Next
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        GetAktTab()
        AktTab.Navigate(TextBox1.Text)
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        GetAktTab()
        AktTab.GoBack()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        GetAktTab()
        AktTab.GoForward()
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        NewTab("Lade...", GetSetting("glumeX", "Settings", "New Tab"))
        GetAktTab()
        AktTab.Navigate(GetSetting("glumeX", "Settings", "Home", "about:blank"))
        GetAktTab()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim parameter As String = Command()
        TabControl1.TabPages.Clear()
        If parameter = "" Then
            NewTab("Lade...", GetSetting("glumeX", "Settings", "Home"))
        Else
            NewTab("Lade...", parameter)
        End If
        GetAktTab()
        Bookmarks.Show()
        Bookmarks.Hide()
        GetBookmarks()
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        GetAktTab()
        If Not TabControl1.TabCount = 1 Then
            Dim sti As Integer
            sti = TabControl1.SelectedIndex
            AktTab.DocumentText = Nothing
            TabControl1.TabPages.Remove(TabControl1.SelectedTab)
            If sti <> TabControl1.TabCount Then
                TabControl1.SelectedIndex = sti
            Else
                TabControl1.SelectedIndex = sti - 1
            End If
        Else
            TextBox1.Text = "about:blank"
            AktTab.Navigate(TextBox1.Text)
        End If
        GetAktTab()
    End Sub

    Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
        If textfocus = False Then
            textfocus = True
            TextBox1.SelectAll()
        End If
    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            GetAktTab()
            AktTab.Navigate(TextBox1.Text)
        End If
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Try
            GetAktTab()
            TextBox1.Text = AktTab.Url.ToString
        Catch
        End Try
        Try
            Dim URL As Uri = New Uri(AktTab.Url.ToString)
            If URL.HostNameType = UriHostNameType.Dns Then
                Dim IconUrl = "http://" & URL.Host & "/favicon.ico"
                Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(IconUrl)
                Dim response As System.Net.HttpWebResponse = request.GetResponse
                Dim stream As System.IO.Stream = response.GetResponseStream
                Dim favicon = Image.FromStream(stream)
                PictureBox1.BackgroundImage = favicon
            End If
        Catch
            PictureBox1.BackgroundImage = Icon.ToBitmap
        End Try
    End Sub

    Private Sub NeuerTabHotkey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NeuerTabHotkey.Click
        NewTab("Lade...", GetSetting("glumeX", "Settings", "Home"))
        GetAktTab()
        AktTab.Navigate(GetSetting("glumeX", "Settings", "Home", "about:blank"))
        GetAktTab()
    End Sub

    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        textfocus = False
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SaveSetting("glumeX", "Settings", "Home", AktTab.Url.ToString)
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        AktTab.Navigate(GetSetting("glumeX", "Settings", "Home", "about:blank"))
    End Sub

    Private Sub LeereSeiteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeereSeiteToolStripMenuItem.Click
        SaveSetting("glumeX", "Settings", "Home", "about:blank")
    End Sub

    Private Sub AktuellerTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AktuellerTabToolStripMenuItem.Click
        SaveSetting("glumeX", "Settings", "Home", AktTab.Url.ToString)
    End Sub

    Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
        Dim oldBookmarks As String = GetSetting("glumeX", "Settings", "Bookmarks")
        Dim NewBookmark As String
        If Not AktTab.DocumentTitle = "" Then
            NewBookmark = InputBox("Bitte geben sie einen Namen einen Namen ein", "Neues Lesezeichen", AktTab.DocumentTitle)
        Else
            NewBookmark = InputBox("Bitte geben sie einen Namen einen Namen ein", "Neues Lesezeichen", AktTab.Url.ToString)
        End If
        If Not NewBookmark = "" Then
            NewBookmark &= "|" & AktTab.Url.ToString & "||"
            SaveSetting("glumeX", "Settings", "Bookmarks", oldBookmarks & NewBookmark)
            GetBookmarks()
        End If
    End Sub

    Private Sub GetBookmarks()
        Bookmarks.ListBox1.Items.Clear()
        Dim Einträge() As String = Split(GetSetting("glumeX", "Settings", "Bookmarks"), "||")
        For i = 0 To Einträge.Length - 1
            If Not Einträge(i) = "" Then
                Bookmarks.ListBox1.Items.Add(Einträge(i))
            End If
        Next
    End Sub

    Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
        GetBookmarks()
        Bookmarks.Open("select")
    End Sub

    Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click
        GetBookmarks()
        Bookmarks.Open("remove")
    End Sub

    Private Sub ToolStripButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton10.Click
        Chronik.Show()
    End Sub
End Class
